
var name = new Ext.form.TextField({
	fieldLabel: 'Nombre',
	allowBlank: false,
	blankText: 'El campo es requerido',
	name: 'name',
	width: 170 
});

var description = new Ext.form.TextArea({
	fieldLabel: 'Descripcion',
	allowBlank: false,
	blankText: 'El campo es requerido',
	name: 'description',
	width: 170,
	height: 200,
});  

var client = new Ext.form.TextField({
	fieldLabel: 'Cliente',
	allowBlank: false,
	blankText: 'El campo es requerido',
	name: 'client',
	width: 170  
});

var start = new Ext.form.DateField({
	fieldLabel: 'Inicio',           
	name: 'start',
	format:'Y-m-d'  
});  

var end = new Ext.form.DateField({
	fieldLabel: 'Fin',           
	name: 'end',
	format:'Y-m-d' 
});

var formPanel = new Ext.form.FormPanel({  
	id: 'add-project-panel',
	frame: true,
	title:'Agregar Proyecto',
	collapsible: true,
	region: 'east',
	labelAlign: 'right',
	labelWidth: 85,
	waitMsgTarget: true,
	url:'/projects',

	items: [
		name, 
		description,
		client,
		start,
		end   
	]
});

formPanel.addButton({
	text: 'Agregar',		
	handler: function(){
		//fs.form.submit({url:'xml-errors.xml', waitMsg:'Saving Data...'});
		if (formPanel.form.isValid()) {
			xmlDocument = '<?xml version="1.0" encoding="UTF-8" ?><project>';
			xmlDocument += '<name>'+name.getValue()+'</name>';
			xmlDocument += '<description>'+description.getValue()+'</description>';
			xmlDocument += '<client>'+client.getValue()+'</client>';
			xmlDocument += '<start>'+start.getRawValue()+'</start>';
			xmlDocument += '<end>'+end.getRawValue()+'</end>';
			xmlDocument += '</project>';
			$.ajax({
			  type: "post",
			  url: "/projects",
			  dataType: "xml",
			  data:{xml: xmlDocument},
			  complete: function(xml){
							if(xml.status != 201) {
								Ext.MessageBox.alert('Exito', 'El Proyecto no pudo ser agregado');								
							} else {
								Ext.MessageBox.alert('Exito', 'El Proyecto fue agregado');
								ds.reload();
							}
						}
			});				
		} else{
				Ext.MessageBox.alert('Errores', 'Por favor, arregla los errores.');
		}
	}
});

